package team.paimengshop.server_socket;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import team.paimengshop.server_database.*;
import team.paimengshop.server_dataclass.*;

import java.io.*;
import java.util.ArrayList;

public class ActionControl_Send
{

    public ActionControl_Send()
    {

    }

    public static JSONArray SendUserInfo(JSONObject jsonObject)
    {
        if(jsonObject.containsKey("userId"))
        {
            int userId = Integer.parseInt(jsonObject.get("userId").toString());
            User user = UserInfoOperate.FindById(userId);
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("refresh", "refresh userInfo");
            jsonObj.put("id", Integer.toString(user.getId()));
            jsonObj.put("userName", user.getUserName());
            jsonObj.put("email", user.getEmail());
            jsonObj.put("password", user.getPassword());
            jsonObj.put("headImageURL", user.getHeadImageURL());
            jsonObj.put("cart", Commodity.commodityArrayListToString(user.getCart()));
            jsonObj.put("collection", Commodity.commodityArrayListToString(user.getCollection()));
            jsonObj.put("address", user.getAddress());
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(jsonObj);
            return jsonArray;
        }
        else if(jsonObject.containsKey("email"))
        {
            String email = jsonObject.get("email").toString();
            User user = UserInfoOperate.FindByEmail(email);
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("refresh", "refresh userInfo");
            jsonObj.put("id", Integer.toString(user.getId()));
            jsonObj.put("userName", user.getUserName());
            jsonObj.put("email", user.getEmail());
            jsonObj.put("password", user.getPassword());
            jsonObj.put("headImageURL", user.getHeadImageURL());
            jsonObj.put("cart", Commodity.commodityArrayListToString(user.getCart()));
            jsonObj.put("collection", Commodity.commodityArrayListToString(user.getCollection()));
            jsonObj.put("address", user.getAddress());
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(jsonObj);
            return jsonArray;
        }
        else
        {
            return new JSONArray();
        }
    }

    public static JSONArray SendCommodityInfo()
    {
        JSONArray jsonArray = new JSONArray();
        ArrayList<Commodity> commodityArrayList = CommodityInfoOperate.GetAllCommodity();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("all commodity", "");
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject);
        for(int i = 0; i < commodityArrayList.size(); i++)
        {
            JSONObject jsonObj = Commodity.CommodityToJson(commodityArrayList.get(i));
            jsonArray.add(jsonObj);
        }
        return jsonArray;
    }

    public static JSONArray SendOrderInfo(JSONObject jsonObject)
    {
        int userId = Integer.parseInt(jsonObject.get("userId").toString());
        JSONArray jsonArray = new JSONArray();
        ArrayList<Order> orderArrayList = OrderOperate.FindByUserId(userId);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("user order", "");
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject1);
        for(int i = 0; i < orderArrayList.size(); i++)
        {
            Order order = orderArrayList.get(i);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("id", Integer.toString(order.getId()));
            jsonObject2.put("userId", order.getUserId());
            jsonObject2.put("address", order.getAddress());
            jsonObject2.put("commodityList", Commodity.commodityArrayListToString(order.getCommodityList()));
            jsonObject2.put("commodityNumber", Integer.toString(order.getCommodityNumber()));
            jsonObject2.put("payment", Double.toString(order.getPayment()));
            jsonArray.add(jsonObject2);
        }
        return jsonArray;
    }

    public static JSONArray Communication(File file)
    {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        byte[] dest = null;
        // 2、选择流
        InputStream is = null;
        // 有新增方法不能发生多态
        ByteArrayOutputStream baos = null;
        try
        {
            is = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            // 3、操作(分段读取)
            byte[] flush = new byte[1024 * 10];// 缓冲容器
            int len = -1;// 接收长度
            try
            {
                while ((len = is.read(flush)) != -1)
                {
                    // 写出到字节数组中
                    baos.write(flush, 0, len);
                }
                baos.flush();
                // 返回回来，上面调用时就有了
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } finally
        {
            // 4、释放资源
            try
            {
                if (null != is)
                {
                    is.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        jsonObject.put("communication image", baos.toByteArray());
        jsonArray.add(jsonObject);
        return jsonArray;
    }

    public static JSONArray Communication(String message)
    {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("communication message", message);
        jsonArray.add(jsonObject);
        return jsonArray;
    }

}
